package com.demo;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.GetMapping;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@EnableTransactionManagement
@Controller
@SpringBootApplication
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}

//	@Bean
//	public OpenEntityManagerInViewFilter openEntityManagerInViewFilter() {
//		return new OpenEntityManagerInViewFilter();
//	}

	@Bean
	public HttpMessageConverters fastJsonMessageConverters() {
		List<HttpMessageConverter<?>> converters = new ArrayList<>();
		//需要定义一个convert转换消息的对象;
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		//添加fastJson的配置信息;
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		fastJsonConfig.setSerializerFeatures(
		        SerializerFeature.WriteMapNullValue,    // 是否输出值为null的字段,默认为false,我们将它打开
		        SerializerFeature.WriteNullListAsEmpty,   // 将Collection类型字段的字段空值输出为[]
		        SerializerFeature.WriteNullStringAsEmpty,  // 将字符串类型字段的空值输出为空字符串
		        SerializerFeature.WriteNullNumberAsZero,  // 将数值类型字段的空值输出为0
		        SerializerFeature.WriteDateUseDateFormat
		);
		//全局时间配置
		fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
		fastJsonConfig.setCharset(Charset.forName("UTF-8"));
		//处理中文乱码问题
		List<MediaType> fastMediaTypes = new ArrayList<>();
		fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		//在convert中添加配置信息.
		fastConverter.setSupportedMediaTypes(fastMediaTypes);
		fastConverter.setFastJsonConfig(fastJsonConfig);
		converters.add(0,fastConverter);
		return new HttpMessageConverters(fastConverter);
	}
	@GetMapping("/")
	public String index() {
		return "index";
	}

}